home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- ============================================================
- Example for sorting dates in the format m/d/yy
- Use with: Dates2.xml
- Notes:-
- 1) The day and month portion of the date can be one or two digits
- therefore it is necessary to pad these with zeroes so that
- final sort select is a constant length numeric.
- ================================================================= -->
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="html" encoding="ISO-8859-1" indent="yes"/>
- <!-- define variable that is used as the separator in dates -->
- <!-- (this saves having to re-work the code for different separators -->
- <xsl:variable name="date-sep" select="'/'"/>
- <xsl:template match="/">
- <html><body>
- <table border="1">
- <tr>
- <th>Sorted Position</th>
- <th>Sort value</th>
- <th>Original date value</th>
- <th>Original position</th>
- </tr>
- <xsl:for-each select="/root/item">
- <!-- sort algorithm -->
- <xsl:sort select="concat(
- substring('00',1,2-string-length(substring-after(substring-after(@date,$date-sep),$date-sep))),substring-after(substring-after(@date,$date-sep),$date-sep),
- concat(substring('00',1,2-string-length(substring-before(@date,$date-sep))),substring-before(@date,$date-sep)),
- concat(substring('00',1,2-string-length(substring-before(substring-after(@date,$date-sep),$date-sep))),substring-before(substring-after(@date,$date-sep),$date-sep))
- )" data-type="number"/>
- <tr>
- <td>
- <xsl:value-of select="position()"/>
- </td>
- <td>
- <!-- the year portion -->
- <xsl:value-of select="concat(substring('00',1,2-string-length(substring-after(substring-after(@date,$date-sep),$date-sep))),substring-after(substring-after(@date,$date-sep),$date-sep))"/>
- <!-- the month portion -->
- <xsl:value-of select="concat(substring('00',1,2-string-length(substring-before(@date,$date-sep))),substring-before(@date,$date-sep))"/>
- <!-- the day portion -->
- <xsl:value-of select="concat(substring('00',1,2-string-length(substring-before(substring-after(@date,$date-sep),$date-sep))),substring-before(substring-after(@date,$date-sep),$date-sep))"/>
- </td>
- <td>
- <xsl:value-of select="@date"/>
- </td>
- <td>
- <!-- show the order of the element in the original xml -->
- <xsl:value-of select="count(preceding-sibling::item)+1"/>
- </td>
- </tr>
- </xsl:for-each>
- </table>
- </body></html>
- </xsl:template>
- </xsl:stylesheet>